//
//  DataBaseSingleton.m
//  豆瓣YHY
//
//  Created by dllo on 15/11/3.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "DataBaseSingleton.h"
#import <sqlite3.h>
#import "Activitylist.h"
@implementation DataBaseSingleton
+ (instancetype)shareDataBase{
    static DataBaseSingleton *dataBase = nil;
    if (dataBase == nil) {
        dataBase = [[DataBaseSingleton alloc]init];
        
    }
    return dataBase;
}
static sqlite3 *db = nil;
//在这里写方法，为了外面的调用
- (void)openDB{
    if (db != nil) {
        NSLog(@"数据库已经打开");
        return;
    }
    NSString *sqlFile = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)lastObject];
    NSString *sqlPath = [sqlFile stringByAppendingPathComponent:@"Activity.sqlite"];
    NSLog(@"sqlPath = %@", sqlPath);
    
    //打开数据库
    //参数1.数据库文件路径
    //参数2.数据库对象
    BOOL result  = sqlite3_open(sqlPath.UTF8String, &db);
    if (result == SQLITE_OK) {
        NSLog(@"打开成功");
    }
    else{
        NSLog(@"打开失败");
    }

}
//创建表
- (void)creatTable{//INTEGET
    NSString *sql = @"CREATE TABLE IF NOT EXISTS lanou(number INTEGER PRIMARY KEY AUTOINCREMENT,title TEXT,begin_time TEXT, address TEXT , wisher_count INTEGET,category_name TXT,participant_count INTEGET)";
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"创建表成功");
    }
}
//查找所有数据
- (void )selectActivity{
    self.array = [NSMutableArray array];
    NSString *sql =  @"SELECT *FROM lanou";
    //创建查询数据库跟随指针
    sqlite3_stmt *stmt = nil;
    //查询前的准备工作
    //参数3.限制sql语句长度，-1为不限制
    //将数据库对象db，sql语句，跟随指针对象stmt关联到一起
    BOOL result = sqlite3_prepare_v2(db, sql.UTF8String, -1, &stmt, nil);
    if (result == SQLITE_OK) {
        NSLog(@"查找准备成功");
    }
    else{
        NSLog(@"查找失败");
    }
    //循环遍历每一行数据
    while (sqlite3_step(stmt) == SQLITE_ROW) {
        //取出第一列数据
//        int number = sqlite3_column_int(stmt, 0);
        //第二列数据是地址
        const unsigned char *title = sqlite3_column_text(stmt, 1);
        //3
        const unsigned char *begin_time  = sqlite3_column_text(stmt, 2);
        //4
        const unsigned char *address = sqlite3_column_text(stmt, 3);
        //5
        const unsigned char *category_name  = sqlite3_column_text(stmt, 4);
        //6
        long wisher_count = sqlite3_column_int(stmt, 5);
        //7
        long participant_count = sqlite3_column_int(stmt, 6);
        //将c语言转oc
   
        NSString *title1 = [NSString stringWithUTF8String:(const char*)title];
        NSString *begin_time1 = [NSString stringWithUTF8String:(const char*)begin_time];
        NSString *address1 = [NSString stringWithUTF8String:(const char*)address];
        NSString *category_name1 = [NSString stringWithUTF8String:(const char*)category_name];
        NSInteger wisher_count1 = wisher_count;
        
       NSInteger participant_count1 = participant_count;
        
        
        Activitylist *act = [[Activitylist alloc]init];
        act.name = title1;
        act.begin_time = begin_time1;
        act.address = address1;
        act.category_name = category_name1;
        act.wisher_count = wisher_count1;
        act.participant_count =participant_count1;
        
        [self.array addObject:act];
        NSLog(@"%@sss",act);


    }
   
    
    
    
}
//插入
- (void)insertStudent:(Activitylist *)activity{
    NSString *sql =  [NSString stringWithFormat:@"INSERT INTO lanou(title,begin_time,address,wisher_count,category_name,participant_count)VALUES('%@','%@','%@','%ld','%@','%ld')",activity.title,activity.begin_time,activity.address,activity.wisher_count,activity.category_name,activity.participant_count];
    BOOL result = sqlite3_exec(db, sql.UTF8String, NULL, NULL, NULL);
    if (result == SQLITE_OK) {
        NSLog(@"插入成功");
    }
    else{
        NSLog(@"插入失败");
    }
}
@end
